package com.inossem.warehousestorage_inossem.activity.storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.storage.StorageManageMatrAdapter;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMaterielIndexBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageMaterielBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.storage.StorageManageMaterielIndexRequest;
import com.inossem.warehousestorage_inossem.request.storage.StorageManageMaterialGetMaterielRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManageMatrActivity extends BaseActivity {

    private TextView title, confirmBtn;

    private PullToRefreshLayout refresh;

    private PullableListView materielListView;

    private StorageManageMatrAdapter adapter;

    private List<StorageManageMatrBean.BodyBean> allData;

    private String mCondition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        //清空sp的物料整理信息
        SPUtils.removeStorageMaterielInformation(this);
        setContentView(R.layout.activity_storage_manage_matr);
        initView(this);
        initData(this);
    }

    @Override
    protected void onResumeImpl() {
        getData(this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielListView = (PullableListView) activity.findViewById(R.id.list);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.refresh);
        confirmBtn = (TextView) activity.findViewById(R.id.confirmBtn);
    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_storage_manage_matr_title);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_storage_manage_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        confirmBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (StorageManageUtils.getStorageMaterielConfirmNumber(activity) == 0) {
                    Utils.middleToast(activity, getResources().getString(R.string.activity_up_frame_overview_commit_prompt));
                } else {
                    Intent intent = new Intent(activity, StorageManageMatrCommitActivity.class);
                    activity.startActivity(intent);
                }
            }
        });
        materielListView.setPullDownFlag(false);
        materielListView.setPullUpFlag(false);
        mCondition = "";
        allData = new ArrayList<>();
        setmScanListener((String scanResult, int length) -> {
            String type = QRCodeUtils.getQRCodeType(scanResult);
            String conditionStr = "";
            switch (type) {
                case QRCodeConstant.TYPE_BATCH:
                    conditionStr = QRCodeUtils.DecryptQRCode(scanResult, QRCodeConstant.PARAMETER_BATCH_NUMBER);
                    break;

                case QRCodeConstant.TYPE_POSITION:
                    conditionStr = QRCodeUtils.DecryptQRCode(scanResult);
                    break;
            }
            getMaterielList(activity, conditionStr, StorageManageUtils.getStorageMaterielBean(activity).getWarehouseId());
        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_POSITION);
    }

    private void getMaterielList(Context context, String conditionStr, String warehouseId) {
        if (TextUtils.isEmpty(conditionStr)) {
            Utils.middleToast(context, context.getResources().getString(R.string.activity_refund_storage_picking_list_the_query_condition_must_not_be_empty));
            return;
        }
        StorageMaterielBean storageBean = StorageManageUtils.getStorageMaterielBean(context);
        if (TextUtils.isEmpty(storageBean.getFactoryId()) || TextUtils.isEmpty(storageBean.getInventoryAddressId())) {
            Utils.middleToast(context, context.getResources().getString(R.string.activity_storage_manage_matr_please_select_factory_and_stock_location));
            return;
        }
        StorageManageMaterialGetMaterielRequest.request(context, conditionStr, warehouseId, new GsonBeanListener<StorageManageMatrBean>() {
            @Override
            public void onSuccess(StorageManageMatrBean bean) {
                mCondition = conditionStr;
                allData = StorageManageUtils.AddMateriel(allData, bean.getBody());
                StorageManageUtils.updateMaterielList(context, allData);
                addMaterielListData(context, StorageManageUtils.getStorageMaterielBean(context), materielListView, true);
            }
        });
    }

    private void addMaterielListData(Context context, StorageMaterielBean bean, ListView listView, boolean flag) {
        if (flag) {
            adapter = new StorageManageMatrAdapter(context, bean, mCondition, new StorageManageMaterielListener() {
                @Override
                public void onNoDoubleClickListener(String condition, String warehouseId) {
                    getMaterielList(context, condition, warehouseId);
                }

                @Override
                public void onDeleteMateriel() {
                    addMaterielListData(context, StorageManageUtils.getStorageMaterielBean(context), materielListView, true);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context) {
        String storageMaterielInformation = SPUtils.getStorageMaterielInformation(context);
        if (TextUtils.isEmpty(storageMaterielInformation)) {
            StorageManageMaterielIndexRequest.request(context, new GsonBeanListener<StorageManageMaterielIndexBean>() {
                @Override
                public void onSuccess(StorageManageMaterielIndexBean bean) {
                    StorageManageUtils.updateFactoryList(context, bean.getBody());
                    StorageManageUtils.updateMaterielList(context, allData);
                    addMaterielListData(context, StorageManageUtils.getStorageMaterielBean(context), materielListView, true);
                }

            });
        } else {
            Gson gson = new Gson();
            StorageMaterielBean bean = gson.fromJson(storageMaterielInformation, StorageMaterielBean.class);
            addMaterielListData(context, bean, materielListView, true);
        }
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(this, this.getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                StorageManageMatrActivity.super.onBackPressed();
            }
        }).show();
    }

    public interface StorageManageMaterielListener {
        void onNoDoubleClickListener(String condition, String warehouseId);

        void onDeleteMateriel();
    }

}
